/****************************************************************
 * <strong>MFDataEdits</strong>
 *
 * <p>
 *MFDataEdits
 * </p>
 *
 * @author AndyZhang 2011 cnet2001@163.com
 * @version 0.1
 * ToDo:2011-11-29
 ***************************************************************/
package com.thinkwide.data.vdata;

import com.thinkwide.data.core.MFString;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class MFDataEdits implements Iterable<MFDataEdit> {

    private ArrayList<MFDataEdit> maps;

    public MFDataEdits() {
        maps = new ArrayList<MFDataEdit>();

    }

    public MFDataEdits clone(MFDataEdits edits) {

        for (MFDataEdit edit : edits) {
            MFDataEdit edit2 = MFDataEdit.clone(edit);
            this.addEdit(edit2);
        }

        return this;
    }

    public void setViewType() {
        for (MFDataEdit col : this) {
            String dataType = col.getDataType();
            if (MFString.compareNoCase(dataType, "date"))
                col.setViewType(MFDataEditViewType.datePicker);
        }
    }

    public MFDataEdit addEdit(MFDataEdit edit) {
        maps.add(edit);
        int i = maps.indexOf(edit);
        edit.setIndex(i);
        return edit;
    }

    public void removeEdit(int index) {
        maps.remove(index);
    }

    public void removeEdit(MFDataEdit edit) {
        maps.remove(edit);

    }

    public void clear() {
        maps.clear();

    }

    public void add(int index, MFDataEdit edit) {
        maps.add(index, edit);

    }

    public int indexOf(MFDataEdit edit) {
        return maps.indexOf(edit);

    }

    public int indexOf(String fieldName) {
        MFDataEdit edit = getEdit(fieldName);
        if (edit == null)
            return -1;
        else
            return maps.indexOf(edit);

    }

    public int count() {
        return maps.size();

    }

    public MFDataEdit getColumn(int index) {

        return maps.get(index);

    }

    public MFDataEdit getEdit(String fieldName) {
        for (MFDataEdit item : maps) {
            if (MFString.compareNoCase(item.getField(), fieldName))
                return item;
        }
        return null;

    }

    public boolean hasEdit(String fieldName) {
        return (getEdit(fieldName) != null);
    }

    public List<String> getColumnNames() {
        List<String> list = new ArrayList<String>();
        for (MFDataEdit item : maps) {
            list.add(item.getField());
        }
        return list;

    }

    public Iterator<MFDataEdit> iterator() {

        Iterator<MFDataEdit> it =
                new Iterator<MFDataEdit>() {
                    private int index = 0;

                    public boolean hasNext() {

                        return index < maps.size();
                    }

                    public MFDataEdit next() {
                        return maps.get(index++);
                    }

                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };

        return it;
    }

}
